// 引入defineStore用于创建store
import { getUserInfo } from '@/api'
import type { User } from '@/types'
import { defineStore } from 'pinia'

// 定义并暴露一个store
export const useUserStore = defineStore('user', {
  // 动作
  actions: {
    fetchUserInfo: async function () {
      if (this.userInfo.id) return
      // if (this.userInfo.id || !localStorage.getItem('accessToken')) return
      const res = await getUserInfo()
      if (res) {
        this.userInfo = res.data
      }
    },
  },
  // 状态
  state: () => ({
    userInfo: {} as User,
  }),
  // 计算
  getters: {},
})
